package com.example.myapplication1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SimpleAdapterActivity extends AppCompatActivity {

    public ListView mListView;
    public List<Map<String,Object>>data = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_adapter);

        mListView=findViewById(R.id.listview2);

        initData();

        String[] from={"name","img","mood"};
        int[] to= {R.id.name,R.id.head ,R.id.mood};

        SimpleAdapter adapter=new SimpleAdapter(this,data,R.layout.simple_item,from,to);

        mListView.setAdapter(adapter);
    }

    private  void initData(){
        Map<String,Object>map1 = new HashMap<>();
        map1.put("name","曹操");
        map1.put("img",R.mipmap.caocao);
        map1.put("mood","宁教天下人负我，休教我负天下人");

        Map<String,Object>map2 = new HashMap<>();
        map2.put("name","甄姬");
        map2.put("img",R.mipmap.zhenji);
        map2.put("mood","zzzz");

        Map<String,Object>map3 = new HashMap<>();
        map3.put("name","司马懿");
        map3.put("img",R.mipmap.simayi);
        map3.put("mood","ccccc");

        Map<String,Object>map4 = new HashMap<>();
        map4.put("name","郭嘉");
        map4.put("img",R.mipmap.guojia);
        map4.put("mood","ddddddd");

        data.add(map1);
        data.add(map2);
        data.add(map3);
        data.add(map4);
    }
}